%------------------------------------------------------------------
%------------------------------------------------------------------
%   Author: Md. Sazzad Hussain (sazzad.hussain@sydney.edu.au)
%   Learning and Affect Technologies Engineering (LATTE)
%   University of Sydney, 2011
%------------------------------------------------------------------
%------------------------------------------------------------------

function [x]=featTrend(inDir,outDir,featList,nameFile,labelList)

filesDir=dir(inDir);%dir info for files path
mkdir(outDir);%create output dir
subNum=0;
r=1;
r2=1;
featAvgAll={};
labelLen=length(labelList);
for m=1:length(filesDir)    %run for all available data files
    [path,name,ext,ver] = fileparts(filesDir(m).name);
    if strcmp(ext,'.mat')   %.mat feature files
        subNum=subNum+1;
        filename=[inDir, '\', name, ext];
        load (filename);
        Data_Set;
        Labels;
        LabelNames=LabelNames';
%         labelList=unique(Labels);%find unique classes
        s1=length(featList);
        featNumAr=[];
        %find the feat index based on featList
        for j=1:s1
            featNum=0;
            for k=1:length(LabelNames)
                featNum=featNum+1;%count the index of feature
                if strcmp(strtrim(featList(j)),strtrim(LabelNames(k)));
                    featNumAr=[featNumAr;featNum]; %store featNum index
                end
            end%LabelNames
        end%featList
        
        %average feat value over labels
        featAvgIndex=[];
        for j=1:length(featNumAr)          
            featAvgClass=[];
            for k=1:length(labelList)
                featAvg=0;
                sumLabels=0;
                for l=1:length(Labels)
                    if strcmp(strtrim(labelList(k)),strtrim(Labels(l)))
                        featVal=Data_Set(l,featNumAr(j));%feat col index
                        featAvg=featAvg+featVal;
                        sumLabels=sumLabels+1;
                    end
                end%Labels
                if sumLabels>0
                featAvgClass=[featAvgClass;(featAvg/sumLabels)];%avg over class
                else
                    featAvgClass=[featAvgClass;0];%avg over class
                end
            end%LabelList
            featAvgIndex=[featAvgIndex,featAvgClass];%avg over feat (all class)
%             featAvgClass=[];
        end%featNumAr
        
        %store records for individual subjects
        %write results to excel
        ascii_xls=66;
        xlswrite([outDir, '\',nameFile],{name},'feat_trend',['A' num2str(r)]);
        xlswrite([outDir, '\',nameFile],labelList,'feat_trend',['A' num2str(r+1)]);        
        for t=1:s1            
            xlswrite([outDir, '\',nameFile],featList(t),'feat_trend',[char(ascii_xls) num2str(r)]);            
            xlswrite([outDir, '\',nameFile],featAvgIndex(:,t),'feat_trend',[char(ascii_xls) num2str(r+1)]);
            ascii_xls=ascii_xls+1;
        end        
        r=(r+2)+length(labelList);

        % %write results in matrix
        ascii_xls=65;%'A'       
        for t=1:s1%featLen            
            xlswrite([outDir, '\',nameFile],featList(t),'feat_mat',[char(ascii_xls) num2str(1)]);            
            xlswrite([outDir, '\',nameFile],labelList','feat_mat',[char(ascii_xls) num2str(2)]); 
            xlswrite([outDir, '\',nameFile],featAvgIndex(:,t)','feat_mat',[char(ascii_xls) num2str(r2+2)]);
            ascii_xls=ascii_xls+(labelLen+1);
        end        
        r2=r2+1;
                
        featAvgAll{subNum}=featAvgIndex;
        featAvgIndex=[];
    end%(.mat/subject)
end%files

%compute final feat avg over subjects
sumFeat=0;
for n=1:subNum
    sumFeat=sumFeat+featAvgAll{n};
end
avgFeatList=sumFeat/subNum;%final feat avg

%write results to excel
ascii_xls=66;
xlswrite([outDir, '\',nameFile],{'allSubjects'},'feat_trend',['A' num2str(r)]);
xlswrite([outDir, '\',nameFile],labelList,'feat_trend',['A' num2str(r+1)]);
for t=1:s1
    xlswrite([outDir, '\',nameFile],featList(t),'feat_trend',[char(ascii_xls) num2str(r)]);
    xlswrite([outDir,'\',nameFile],avgFeatList(:,t),'feat_trend',[char(ascii_xls) num2str(r+1)]);
    ascii_xls=ascii_xls+1;
end

x=1;